﻿using System;
using System.Web;
using System.Data;
using System.Text;
using VIT.DataHelper.DataAccess;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections.Generic;

namespace VIT.Web.Modules.FavouriteProduct
{
    public class Prpduct
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Properties
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public int ManufacturerId { get; set; }
        public int SupplierId { get; set; }
        public string LanguageId { get; set; }
        public string Type { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Brief { get; set; }
        public string Description { get; set; }
        public string Tag { get; set; }
        public decimal Price { get; set; }
        public decimal Sale { get; set; }
        public int Views { get; set; }
        public int Orders { get; set; }
        public bool Published { get; set; }
        #endregion

        #region Constructor
        public Prpduct(int id = -1,
                        int categoryId = 0,
                        int manufacturerId = 0,
                        int supplierId = 0,
                        decimal price = 0,
                        decimal sale = 0,
                        int views = 0,
                        string languageId = "",
                        string image = "",
                        string title = "", 
                        string brief = "",
                        string description = "",
                        string tag = "",
                        string type="",
                        int orders = 0,
                        bool published = true)
        {
            this.Id = id;
            this.CategoryId = categoryId;
            this.ManufacturerId = manufacturerId;
            this.SupplierId = supplierId;
            this.LanguageId = languageId;
            this.Type = type;
            this.Image = image;
            this.Title = title;
            this.Brief = brief;
            this.Description = description;
            this.Price = price;
            this.Sale = sale;
            this.Views = views;
            this.Tag = tag;
            this.Orders = orders;
            this.Published = published;
        }
        #endregion

        #region Pre
        public DataTable getTopViewProduct(Dictionary<string,string> param)
        {
            int Top;
            int.TryParse(param["Top"], out Top);
            return getTopViewProduct(Top);
        }
        private DataTable getTopViewProduct(int top = 0)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select ");
            if (top != 0)
            {
                strQuery.Append("top ");
                strQuery.Append(top);
            }
            strQuery.Append(" id, Title, Image, Brief, Description, Price, Sale, Tag, Views FROM Products, Products_Language where Id=ProductId and LanguageId=@LanguageId and Published=1 ");
            strQuery.Append("order by Views/(DATEDIFF(day,GETDATE()+1,Products_Language.ModifyDate))");

            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            return sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId).Tables[0];
        }
        #endregion
    }
}
